package org.tyq.websocket;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by 谭永强 on 2016/2/16.
 */
@ServerEndpoint(value = "/socket")
public class Websocket {

    private static Map<String, Session> sessionMap = new HashMap<String, Session>();

    @OnMessage
    public void onMessage(String message,Session session) throws IOException, InterruptedException {
        String id = session.getId();
        String name = "游客";
        if("0".equals(id)){
            name = "大雄";
        }else if("1".equals(id)){
            name = "静香";
        }else if("2".equals(id)){
            name = "小强";
        }else if("3".equals(id)){
            name = "胖虎";
        }
        // 服务器向客户端发送数据
        sendAll(name,message);
    }

    /**
     * 向所有人发送消息
     * @throws IOException
     */
    public static void sendAll(String name, String message) throws IOException {
        Set<Map.Entry<String,Session>> entrySet = sessionMap.entrySet();
        for (Map.Entry<String, Session> entry : entrySet) {
            entry.getValue().getBasicRemote().sendText("{'name':'"+name+"','text':'"+message+"'}");
        }
    }

    @OnOpen
    public void onOpen(Session session) {
        // 打开连接
        sessionMap.put(session.getId(), session);
        System.out.println("打开服务器连接");
    }

    @OnClose
    public void onClose(Session session) {
        // 关闭连接
        sessionMap.remove(session.getId());
        System.out.println("服务器连接已关闭");
    }
}
